jQuery.fn.exists = function(){return this.length>0;}

var LING_POR = {

    initMoreLess : function() {
        $('.read-more').bind('click', function(e) {
            e.preventDefault();
            var parentHolder  = $(this).parent();
            parentHolder.hide();
            parentHolder.next().show();
        });

        $('.read-less').bind('click', function(e) {
            e.preventDefault();
            var parentHolder  = $(this).parent();
            parentHolder.hide();
            parentHolder.prev().show();
        });
    },

    initErrors : function() {
        $('.error .close').bind('click', function(e) {
            $(this).parents('.error').slideUp();
        });
    },

    initApplyForm: function() {
        var providerGetHolder = $("#provider-get");
        var bankfee = providerGetHolder.data('bankfee');
        var usfee = providerGetHolder.data('usfee');
        var textHolder = $("#provider-get").next('p')
        $("#provider-price").keyup(function () {
            this.value = this.value.replace(/[^0-9\.]/g,'');
            if($.isNumeric(this.value)) {
                $("span:eq(0)", textHolder).text((bankfee*(this.value)).toFixed(2))
                $("span:eq(1)", textHolder).text((usfee*(this.value)).toFixed(2))
                providerGetHolder.val((this.value-(bankfee*(this.value))-(usfee*(this.value))).toFixed(2))
            } else {
                $("span:eq(0)", textHolder).text('')
                $("span:eq(1)", textHolder).text('')
                providerGetHolder.val('')
            }
        });

        $('#applyForm .form-actions button').unbind('click').bind('click', function(e) {
            e.preventDefault();
            var that = $(this);
            $.post($('#applyForm form').data('href'), $('#applyForm form').serialize(), function(data) {
                if(data === 'error') {
                    $('#provider-open .error').slideDown();
                } else {
                    $('#provider-open .error').slideUp();
                    $('#order-'+data.id).remove();
                    $('#provider-applied .error').after(data.source);
                    $('#order-'+data.id).effect("highlight", {color: '#dff0d8'}, 3000);
                    LING_POR.initProviderOpenOrders();
                    LING_POR.initMoreLess();
                }
            }).fail(function() {
                    $('#provider-open .error').slideDown();
                }).always(function() {
                    $.fancybox.close();
                });
        });

    },

    initiDatepickers : function() {

        var nowTemp = new Date();
        var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

        var canFinish;

        var canStart = $('#dp3').datepicker({
            onRender: function(date) {
                return date.valueOf() < now.valueOf() ? 'disabled' : '';
            }
        }).on('changeDate', function(ev) {
                if (ev.date.valueOf() > canFinish.date.valueOf()) {
                    var newDate = new Date(ev.date)
                    newDate.setDate(newDate.getDate() + 1);
                    canFinish.setValue(newDate);
                }
                canStart.hide();
                canFinish.show();
            }).data('datepicker');

        canFinish = $('#dp4').datepicker({
            onRender: function(date) {
                return date.valueOf() < canStart.date.valueOf() ? 'disabled' : '';
            }
        }).on('changeDate', function(ev) {
                canFinish.hide();
            }).data('datepicker');
    },

    initProviderOpenOrders : function() {

        LING_POR.initApplyForm();

        $('#provider-remove').unbind('click').bind('click', function(e) {
            e.preventDefault();
            var that = $(this);
            $.post($(this).attr('href'), function(data) {
                if(data === 'error') {
                    $('#provider-applied .error').slideDown();
                } else {
                    $('#provider-applied .error').slideUp();
                    $('#order-'+data.id).remove();
                    $('#provider-open .error').after(data.source);
                    $('#order-'+data.id).effect("highlight", {color: '#dff0d8'}, 2000);
                    LING_POR.initProviderOpenOrders();
                    LING_POR.initMoreLess();
                }
            }).fail(function() {
                    $('#provider-applied .error').slideDown();
                });
        });

    }

}

$( document ).ready(function() {
    LING_POR.initProviderOpenOrders();
    LING_POR.initErrors();
    LING_POR.initMoreLess();
    LING_POR.initiDatepickers();


    $("#applyBox").fancybox({
        type     :"inline",
        closeBtn :true,
        beforeLoad : function(){
            var url= $(this.element).data("href");
            $('form', this.content).data('href', url);
        },
        afterClose : function() {
            var providerPriceHolder = $('form #provider-price', this.content);
            var providerGetHolder = $('form #provider-get', this.content);
            providerGetHolder.val(providerGetHolder.data('value'));
            providerPriceHolder.val(providerPriceHolder.data('value'));

            var bankfee = providerGetHolder.data('bankfee');
            var usfee = providerGetHolder.data('usfee');
            var textHolder = $("#provider-get").next('p');
            $("span:eq(0)", textHolder).text((bankfee*(providerPriceHolder.data('value'))).toFixed(2));
            $("span:eq(1)", textHolder).text((usfee*(providerPriceHolder.data('value'))).toFixed(2));
        }
    });


});